﻿
var api = angular.module('api',[]);
var URL = './';
var baseUrl = "http://61.155.85.77:8999";
var data_jsons = {"field":[]};
var field_cn_dz = {};//中英文对照
var modelId = 2;
var temp_option = {temp:[]};

api.factory('commonApi',['$rootScope','$timeout','$http',function($rootScope,$timeout,$http){
	//封装请求接口
	function _run(url,params,onSuccess) {
		$http({
			url: url,
			method: "get",
			data: params
		}).success(onSuccess).error(function(){
			alert('error!');
		});
	};
	
	function getJSONData(url, data, callback) {
		$.ajax({
			type: "get",
			async: true,
			data: data,
			url: url, //这里是网址
			success: function(json) {
				callback(json)
			},
			timeout: 30000,
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				console.error(url, textStatus + ": " + errorThrown);
			}
		});
	}
	
	function postJSONData(url, data, callback) {
		$.ajax({
			type: "post",
			async: true,
			data: data,
			url: url, //这里是网址
			success: function(json) {
				callback(json)
			},
			timeout: 30000,
			error: function(XMLHttpRequest, textStatus, errorThrown) {
				console.error(url, textStatus + ": " + errorThrown);
			}
		});
	}
	
	//获取测试数据
	this.getFieldData = function(params,onSuccess){
            
		var url = URL + 'js/field.json';
		
		_run(url,params,onSuccess);
	};
	
	this.getModelList = function(callback){
		var url = baseUrl+"/getModelList";
		getJSONData(url, {}, function(data){
			if(typeof callback == "function"){
				callback(data);
			}
		})
	}
	
	this.getViewList = function(callback){
		var url = baseUrl+"/getViewList?modelId="+modelId;
		getJSONData(url, {}, function(data){
			if(typeof callback == "function"){
				callback(data);
			}
		})
	}
	
	this.getFieldList = function(obj,callback){
		var that = this;
		if(obj){
			var $obj = $(obj);
		}else{
			var $obj = $(".title-desc select#vw");
		}
		var value = $obj.val();
		var viewName = value;
		var url = baseUrl+"/getFieldList?modelId="+modelId+"&viewName="+viewName;
		getJSONData(url, {}, function(data){
			if(data.success){
				that.getResult(viewName,data.data,callback)
			}
		})
	}
	
	this.getResult = function(viewName,fieldNames,callback){
		$.each(fieldNames, function(i,e) {
			var url = baseUrl+"/getResult?modelId="+modelId+"&viewName="+viewName+"&fieldName="+e.fieldName+"&fieldNameCn="+(e.fieldNameCn.replace('%','%25'));
			getJSONData(url, {}, function(data){
				if(data.success){
					//console.log(data);
					var fieldName = "";
					var fieldNameEn = "";
					var fieldArr = [];
					var fieldType = "";
					$.each(data.data,function(j,f){
						fieldNameEn = f.fieldName;
						fieldName = f.fieldNameCn;
						fieldType = f.fieldType?f.fieldType:"string";
						fieldArr.push(f.value);
					})
					
					if(!field_cn_dz[fieldName]){
						field_cn_dz[fieldName] = [];
						data_jsons.field.push({
							data:fieldArr,
							data_type:fieldType,
							title:fieldName,
							fieldName:fieldNameEn,
							modelId:modelId,
							viewName:viewName
						});
						if(typeof callback == "function"){
							callback(data_jsons);
						}
					}
				}
			})
		});
	}
	
	this.fabuPost = function(params,callback){
		var url = baseUrl+"/fabuHtml"
		postJSONData(url, params, function(data){
			if(typeof callback == "function"){
				callback(data);
			}
		})
	}
	
	this.previewPost = function(params,callback){
		var url = baseUrl+"/preViewHtml"
		postJSONData(url, params, function(data){
			if(typeof callback == "function"){
				callback(data);
			}
		})
	}
	
	this.resetScope = function($scope,keyName){
		$scope.$apply(function() {
			$scope.data[keyName] = [];
		});
	}
	
	this.resetPage = function($scope){
		data_jsons.field = [];
		field_cn_dz = {};
		this.resetScope($scope,'fieldDate');
		this.resetScope($scope,'chart_x');
		this.resetScope($scope,'chart_y');
		$('#chart').empty().append("<p ng-hide=\"$scope.data.charts\">当前图表无数据</p>");
		/*if(echarts.getInstanceByDom($('#chart')[0])){
			echarts.getInstanceByDom($('#chart')[0]).clear();
		}*/
	}
	
	window.commonApi = this;
	return this;
}]);

